package 字节码文件.控制转移指令;

import org.junit.Test;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO: 控制转移指令
 * <P> Date: 2022/3/27:17:11
 * <P> Description:
 */

public class IfSwitchGotoTest {
    /**
     * TODO:1.条件跳转指令
     **/

    /**
     * TODO: 0 iconst_0
     *  1 istore_1
     *  2 iload_1
     *  3 ifne 12 (+9)
     *  6 bipush 10
     *  8 istore_1
     *  9 goto 15 (+6)
     * 12 bipush 20
     * 14 istore_1
     * 15 return
     **/
    public void compare1() {
        int a = 0;
        if (a == 0) {
            a = 10;
        } else {
            a = 20;
        }

    }

    public boolean compareNull(String str) {
        if (str == null) {
            return true;
        } else {
            return false;
        }
        //1 是true
        //0 是false
    }

    public void compare2() {
        float f1 = 9;
        float f2 = 10;
        System.out.println(f1 < f2);

    }

    public void compare3() {
        int i1 = 10;
        long l1 = 20;
        // i2l 比较的时候把int 提示为long
        System.out.println(i1 < l1);
    }

    public int compare4(double d) {
        if (d > 50.0) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * TODO:2.比较条件跳转指令
     **/
    public void ifCompare1() {
        int i = 10;
        int j = 20;
        System.out.println(i < j);
        System.out.println(i > j);
    }

    public void ifCompare2() {
        short s1 = 10;
        byte b1 = 20;
        System.out.println(s1 < b1);
    }

    public void ifCompare3() {
        Object obj1 = new Object();
        Object obj2 = new Object();

        System.out.println(obj1 == obj2);//false
        System.out.println(obj1 != obj2);//true
    }

    /**
     * TODO:3.多条件分支跳转
     **/
    public void switch1(int select) {
        int num;
        switch (select) {
            case 1:
                num = 10;
                break;
            case 2:
                num = 20;
                break;
            case 3:
                num = 30;
                break;
            default:
                num = 40;
        }
    }

    //会对case-offset 进行排序优化 更好匹配 但是代码顺序不影响

    /**
     * TODO: 0 sipush 500
     *  3 istore_2
     *  4 iload_2
     *  5 lookupswitch 3
     * 100:  40 (+35)
     * 200:  49 (+44)
     * 500:  46 (+41)
     * default:  55 (+50)
     * 40 bipush 10
     * 42 istore_1
     * 43 goto 58 (+15)
     * 46 bipush 20
     * 48 istore_1
     * 49 bipush 30
     * 51 istore_1
     * 52 goto 58 (+6)
     * 55 bipush 40
     * 57 istore_1
     * 58 getstatic #4 <java/lang/System.out : Ljava/io/PrintStream;>
     * 61 iload_1
     * 62 invokevirtual #11 <java/io/PrintStream.println : (I)V>
     * 65 return
     **/
    public void switch2(int select) {
        int num;
        switch (select) {
            case 100:
                num = 10;
                break;
            case 500:
                num = 20;
                break;
            case 200:
                num = 30;
                break;
            default:
                num = 40;
        }
    }

    //jdk1.7新特性 字符串 对匹配case 进行hash计算 如果相同 在调用传入的字符串进行equals比较
    public void switch3(String season) {
        switch (season) {
            case "SPRINT":
                break;
            case "SUMMER":
                break;
            case "AUTUMN":
                break;
            case "WINTER":
                break;
        }
    }

    /**
     * TODO:4.无条件跳转
     **/

    public void whileIne() {
        int i = 0;
        while (i < 100) {
            String s = "atguigu.com";
            i++;
        }
    }

    public void whileDouble() {
        double d = 0.0;
        while (d < 100.1) {
            String s = "atguigu.com";
            d++;
        }
        //先取出对应值 然后用变量1 进行add 最后放入局部变量表
        //13 dload_1
        //14 dconst_1
        //15 dadd
        //16 dstore_1
    }

    public void printFor() {
        short i = 0;
        for (i = 0; i < 100; i++) {
            String s = "atguigu.com";
        }
        //先取出对应值 然后用变量1 进行add 在窄化类型转换 i2s 最后放入局部变量表
        //13 iload_1
        //14 iconst_1
        //15 iadd
        //16 i2s
        //17 istore_1
    }


    public void whileTest() {
        int i = 1;
        while (i <= 100) {
            i++;
        }
        //可以继续使用i
    }

    public void forTest() {
        for (int i = 1; i <= 100; i++) {

        }
        //不可以继续使用i
    }

    //do_while 先自增 然后再判断
    public void doWhileTest() {
        int i = 1;
        do {
            i++;
        } while (i <= 100);
    }


}
